//
//  FZHomeDetailCategoryModel.m
//  Pods
//
//  Created by CyonLeuPro on 15/5/19.
//
//

#import "FZHomeCourseModel.h"

@implementation FZHomeCourseModel

+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    NSMutableDictionary *dict = [[super JSONKeyPathsByPropertyKey] mutableCopy];
    
    NSDictionary *addDict =  @{
                               @"identifier": @"book_id",
                               @"title": @"name",
                               @"subtitle": @"sub_title",
                               @"desc": @"description",
                               
                               @"zip": @"zip",
                               @"audioURL": @"audio",
                               @"pic": @"pic",
                               
                               @"views": @"views",
                               @"createTime": @"update_time",
                               @"gradeID": @"grade_id",
                               @"isVIP":@"is_vip",
                               
                               @"albumID": @"album_id",
                               @"isAlbum": @"isalbum",
                               @"isCollected": @"is_collect",
                               @"courseNum": @"course_num",
                               
                               @"downloadState": @"downloadState",
                               @"dowloadProgress": @"dowloadProgress",
                               
                               @"lastPageID": @"lastPageID",
                               @"lastPageIndex": @"lastPageIndex",
                            
                               
                               @"isNeedBuy": @"is_needbuy"
                               
                               };
    
    [dict addEntriesFromDictionary:addDict];
    
    return dict;
}


- (instancetype)initWithCoder:(NSCoder *)coder {
    self = [super initWithCoder:coder];
    if (self) {
        self.identifier = [coder decodeObjectOfClass:[NSString class] forKey:@"book_id"];
        self.title = [coder decodeObjectOfClass:[NSString class] forKey:@"name"];
        self.subtitle = [coder decodeObjectOfClass:[NSString class] forKey:@"sub_title"];
        self.desc = [coder decodeObjectOfClass:[NSString class] forKey:@"description"];
        
        self.zip = [coder decodeObjectOfClass:[NSString class] forKey:@"zip"];
        self.audioURL = [coder decodeObjectOfClass:[NSString class] forKey:@"audio"];
        self.pic = [coder decodeObjectOfClass:[NSString class] forKey:@"pic"];
        
        self.views = [coder decodeIntegerForKey:@"views"];
        self.createTime = [coder decodeObjectOfClass:[NSString class] forKey:@"update_time"];
        self.gradeID = [coder decodeObjectOfClass:[NSString class] forKey:@"grade_id"];
        self.gradeName = [coder decodeObjectOfClass:[NSString class] forKey:@"gradeName"];
        self.isVIP = [coder decodeIntegerForKey:@"is_vip"];
        
        self.albumID = [coder decodeObjectOfClass:[NSString class] forKey:@"album_id"];
        self.isAlbum = [coder decodeIntegerForKey:@"isalbum"];
        self.isCollected = [coder decodeIntegerForKey:@"is_collect"];
        self.courseNum = [coder decodeIntegerForKey:@"course_num"];
        self.isNeedBuy = [coder decodeObjectForKey:@"is_needbuy"];
        self.downloadState = [coder decodeIntegerForKey:@"downloadState"];
        self.dowloadProgress = [coder decodeIntegerForKey:@"dowloadProgress"];
        self.isSelected = NO;
        
        self.lastPageID = [coder decodeObjectForKey:@"lastPageID"];
//        self.lastPageIndex = [coder decodeIntegerForKey:@"lastPageIndex"];
        
        self.destinationBlock = nil;
        self.progressBlock = nil;
        self.successBlock = nil;
        self.failureBlock = nil;
    }
    
    return self;
}

- (void)encodeWithCoder:(NSCoder *)coder {
    [super encodeWithCoder:coder];
    
    [coder encodeObject:self.identifier forKey:@"book_id"];
    [coder encodeObject:self.title forKey:@"name"];
    [coder encodeObject:self.subtitle forKey:@"sub_title"];
    [coder encodeObject:self.desc forKey:@"description"];
    
    [coder encodeObject:self.zip forKey:@"zip"];
    [coder encodeObject:self.audioURL forKey:@"audio"];
    [coder encodeObject:self.pic forKey:@"pic"];
    
    [coder encodeInteger:self.views forKey:@"views"];
    [coder encodeObject:self.createTime forKey:@"update_time"];
    [coder encodeObject:self.gradeID forKey:@"grade_id"];
    [coder encodeObject:self.gradeName forKey:@"gradeName"];
    [coder encodeInteger:self.isVIP forKey:@"is_vip"];
    
    [coder encodeObject:self.albumID forKey:@"album_id"];
    [coder encodeInteger:self.isAlbum forKey:@"isalbum"];
    [coder encodeInteger:self.isCollected forKey:@"is_collect"];
    [coder encodeInteger:self.courseNum forKey:@"course_num"];
    [coder encodeObject:self.isNeedBuy forKey:@"is_needbuy"];
    [coder encodeInteger:self.downloadState forKey:@"downloadState"];
    [coder encodeInteger:self.dowloadProgress forKey:@"dowloadProgress"];
    [coder encodeObject:self.lastPageID forKey:@"lastPageID"];
    [coder encodeInteger:self.lastPageIndex forKey:@"lastPageIndex"];
    
    [coder encodeObject:nil forKey:@"destinationBlock"];
    [coder encodeObject:nil forKey:@"progressBlock"];
    [coder encodeObject:nil forKey:@"successBlock"];
    [coder encodeObject:nil forKey:@"failureBlock"];
}

//+ (NSValueTransformer *)isVipJSONTransformer{
//    return [NSValueTransformer valueTransformerForName:MTLBooleanValueTransformerName];
//}

+ (NSValueTransformer *)downloadStateJSONTransformer{
    return [MTLValueTransformer reversibleTransformerWithForwardBlock:^id(NSString *string) {
        return @([string integerValue]);
    } reverseBlock:^id(NSNumber *number) {
        return [number stringValue];
    }];
}

+ (NSValueTransformer *)lastPageIndexJSONTransformer{
    return [MTLValueTransformer reversibleTransformerWithForwardBlock:^id(NSString *string) {
        return @([string integerValue]);
    } reverseBlock:^id(NSNumber *number) {
        return [number stringValue];
    }];
}

+ (NSValueTransformer *)dowloadProgressJSONTransformer{
    return [MTLValueTransformer reversibleTransformerWithForwardBlock:^id(NSString *string) {
        return @([string floatValue]);
    } reverseBlock:^id(NSNumber *number) {
        return [number stringValue];
    }];
}
@end
